<template>
  <div></div>
</template>

<script>
export default {
  name: 'AppVersion',
  data: function () {
    return {
      response: {
          fileUrl: '',
          currVersion: '',
          currVersionCode: '',
          info: '',
          isForce: true,
      },
      downloadTask: null,
      downloadUrl: 'https://yangfan-shop.oss-cn-hangzhou.aliyuncs.com/apk/yangfan-member-1.0.89.apk',
      dialog: false,
    }
  },
  beforeDestroy () {
    if (this.downloadTask && typeof this.downloadTask.abort === 'function') {
      this.downloadTask.abort()
    }
  },
  mounted () {
    // var response = {
    //   fileUrl: 'https://yangfan-shop.oss-cn-hangzhou.aliyuncs.com/apk/yangfan-member-1.0.89.apk',
    //   currVersion: '1.0.90',
    //   currVersionCode: '90',
    //   info: '当前更新内容: <br/>1.修复主页UI异常 <br/>2.新增unipush消息推送',
    //   isForce: true,
    // }
    let $this=this
    this.bus.$on('AppVersionCallBus',function () {
      // $this.checkAppVersion($this.check)
    })
    // $this.checkAppVersion($this.check)
  },
  methods: {
    check: function (response) {
      // console.log('response',response)
      // this.updateUrl = res.data.Data
      let $this = this
      $this.response=response
      var AppVersionCode = localStorage.getItem('AppVersionCode') - 0
      // console.log('AppVersionCode',AppVersionCode)
      if (typeof plus==='undefined'){
        return
      }
      if (AppVersionCode < response.currVersionCode) {
        plus.nativeUI.confirm('发现新版本: ' + response.currVersion + ',是否下载新版本?', function (e) {
          // console.log('User pressed!: ' + e.index)
          if (e.index === 0) {
            if ($this.getPlatform()==='Android'){
              // $this.startDownload()
              plus.runtime.openURL(response.fileUrl)
            }
            if ($this.getPlatform()==='iOS'){
              plus.runtime.openURL(response.fileUrl)
              // $this.startDownload()
            }
          }else{
            plus.nativeUI.toast("您选择继续使用当前旧版")
          }
        }, '系统提示', ['确认下载', '忽略当前版本'])
      }
    },
    startDownload: function () {
      let $this=this
      plus.nativeUI.showWaiting("安装文件,下载中...");
      var dtask = plus.downloader.createDownload(
        $this.response.fileUrl,
        {
          method: "GET"
        }, (d, status) => {
          console.log(d);
          if (status == 200) {
            plus.nativeUI.closeWaiting();
            plus.nativeUI.toast("下载成功安装，准备安装")
            setTimeout(function () {
              plus.runtime.install(d.filename)
            },2000)
          } else {
            plus.nativeUI.alert("安装失败，请稍候重试: " + status)
          }
        });
      dtask.start();
      console.log('开始下载')
    }
  }
}
</script>

<style scoped>

</style>
